package com.example.smartcitydemo.base

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment

/**
 * @PACKAGE_NAME: com.example.smartcitydemo.base
 * @NAME: Caleb.F
 * @DATE: 2021/11/5 15:24
 * @Email: 1919796283@qq.com
 */
abstract class BaseFragment<VB : ViewDataBinding>(@LayoutRes val layoutId: Int) : Fragment() {
    protected lateinit var myContext: Context
    protected val TAG = this::class.java.simpleName


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val inflate = inflater.inflate(layoutId, container, false)
        return inflate
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        myContext = context
        initData()
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val bindView: VB = DataBindingUtil.bind<VB>(view)!!
        initView(bindView, savedInstanceState)
        addObservers(bindView)
    }

    /**
     * 加载基础数据
     */
    abstract fun initData()

    /**
     * 初始化View
     */
    abstract fun initView(bindView: VB, savedInstanceState: Bundle?)

    /**
     * 添加数据观察者
     */
    abstract fun addObservers(bindView: VB)
}